$(function(){
	var jqueryMap, fnsMap,
		isClick = false,
		dateInfo = null;
	
	jqueryMap = {
		$maskSection : $('#mask-section'),
		$monthGroup: $('.mask-month-group'),
		$dateContent : $('#date-content'),
		$varietyBtn : $('.variety-btn'),
		$varietySection : $('#variety-section'),
		$calendarSection : $('#calendar-section')
	};

	fnsMap = {
		MobileDate : function (opts) {
			var _date = (new MobileDate).init(opts);

			dateInfo = _date;
			return _date;
		},
		dateInit : function (opts) {
			var nowDate = this.MobileDate(opts);

			this.createDateHeadHtml(nowDate);
			this.createDateTable(nowDate);
		},
		createDateHeadHtml : function (data) {
			var template_html = template('date-head-template', data);

			jqueryMap.$calendarSection.find('.calendar-header').html(template_html);
		},
		// @param {Number} dayNum 选择到的某一天数
		createDateTable : function (data) {
			var template_html = template('date-template', data);

			jqueryMap.$dateContent.html(template_html);
		},
		createMonthData : function (arr, month) {
			var single_obj = {},
				single_arr = [],
				result_arr = [];
			
			for (var i = 0, len = arr.length; i < len; i++) {
				
				single_obj = {
					id: i + 1,
					text: arr[i],
					isActive: arr[i] === month
				}

				single_arr.push(single_obj);
			}
			
			for (var i = 0, len = single_arr.length / 3; i < len; i++) {
				result_arr.push(single_arr.splice(0,3));
			}

			return result_arr;
		}
	};
	
	fnsMap.dateInit();
	
	jqueryMap.$calendarSection.on('touchstart', '.calendar-month', function() {
		var btn_month = $(this).find('span').text(),
			month_arr = dateInfo.monthArr,
			month_data = fnsMap.createMonthData(month_arr, btn_month),
			month_html = template('mask-month-template', {list: month_data});
		
		jqueryMap.$monthGroup.html(month_html);
		jqueryMap.$maskSection.addClass('fade-in').removeClass('fade-out');
		
		return false;
	});
	
	jqueryMap.$maskSection.on('touchstart', '.btns', function() {
		jqueryMap.$monthGroup.find('.btns').removeClass('active');
		$(this).addClass('active');
		
		return false;
	});
	
	jqueryMap.$maskSection.on('touchstart', '.cancel', function() {
		jqueryMap.$maskSection.addClass('fade-out').removeClass('fade-in');
		
		return false;
	});
	
	jqueryMap.$maskSection.on('touchstart', '.confirm', function() {
		var now_year = $('#hidden-year').val(),
			now_month,
			now_day = $('#hidden-day').val();
			
		jqueryMap.$monthGroup.find('.btns').each(function(){
			if ($(this).hasClass('active')) {
				now_month = $(this).data('month');
			}
		});
		
		fnsMap.dateInit({
			month: now_month,
			day: now_day
		});
		
		jqueryMap.$maskSection.addClass('fade-out').removeClass('fade-in');
		
		return false;
	});
	
	jqueryMap.$varietyBtn.on('touchstart', function() {
		var $Section = jqueryMap.$varietySection,
			$content = $Section.find('.variety-content');
		
		if ($(this).hasClass('up')) {
			$(document).on('touchmove',function(){
				return false;
			});
			$Section.addClass('fade-in').removeClass('fade-out');
			$content.addClass('fade-up').removeClass('fade-down');
			$content.removeAttr('style');
			isClick = true;
		}
		
		if ($(this).hasClass('down')) {
			$(document).off('touchmove');
			$Section.removeClass('fade-in').addClass('fade-out');
			$content.removeClass('fade-up').addClass('fade-down');
			$content.removeAttr('style');
			isClick = false;
		}
		
		return false;
	});
	
	jqueryMap.$varietySection.on('webkitAnimationEnd', function(e) {
		var animationName = e.animationName,
			$content = $(this).find('.variety-content');
	});
	
	jqueryMap.$dateContent.on('touchstart', '.item', function(){
		var $this = $(this),
			now_month = $('#hidden-month').val(),
			now_day = parseInt($this.text(), 10),
			date_info = fnsMap.MobileDate({
				month: now_month,
				day: now_day
			});
		
		if ($this.hasClass('prohibit') || $this.hasClass('active') || $this.hasClass('untouch')) {
			return false;
		}
		
		jqueryMap.$dateContent.find('.item').removeClass('active');
		$this.addClass('active');
		
		fnsMap.createDateHeadHtml(date_info);
	});
	
	jqueryMap.$varietySection.on('touchstart', '.btns', function() {
		var now_name = $(this).attr('name'),
			now_year = $('#hidden-year').val(),
			now_month = $('#hidden-month').val(),
			now_day = $('#hidden-day').val(),
			now_date = now_year + '-' + now_month + '-' + now_day,
			_url = 'market.html?publishTime='+ now_date +'&tag='+ now_name +'&type=3';
		
		$(this).addClass('active');
		window.location.href = _url;
	});
});